package cn.toys.base.utils;

import cn.toys.base.exception.BusinessException;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SpringBeanSupport {


    /**
     * 仅接收字符串,数字类型,日期类型的封装数据
     *
     * @param typeClz
     * @return
     */
    private static boolean isCanBeFill(Class<?> typeClz) {
        return String.class.isAssignableFrom(typeClz)
                || Number.class.isAssignableFrom(typeClz)
                || Date.class.isAssignableFrom(typeClz);
    }

    private static void fillFieldValue(JSONObject paramIn, Object data, Field field) {
        String name = field.getName();
        try {
            Class<?> typeClz = field.getType();
            if (typeClz.isAssignableFrom(String.class)) {
                field.set(data, paramIn.getString(name));
            } else if (typeClz.isAssignableFrom(Integer.class)) {
                field.set(data, paramIn.getInteger(name));
            } else if (typeClz.isAssignableFrom(Long.class)) {
                field.set(data, paramIn.getLong(name));
            } else if (typeClz.isAssignableFrom(Double.class)) {
                field.set(data, paramIn.getDouble(name));
            } else if (typeClz.isAssignableFrom(Short.class)) {
                field.set(data, paramIn.getShort(name));
            } else if (typeClz.isAssignableFrom(Float.class)) {
                field.set(data, paramIn.getFloat(name));
            } else if (typeClz.isAssignableFrom(BigDecimal.class)) {
                field.set(data, paramIn.getBigDecimal(name));
            } else if (typeClz.isAssignableFrom(BigInteger.class)) {
                field.set(data, paramIn.getBigInteger(name));
            } else if (typeClz.isAssignableFrom(Timestamp.class)) {
                field.set(data, paramIn.getTimestamp(name));
            } else if (typeClz.isAssignableFrom(Date.class)) {
                field.set(data, paramIn.getDate(name));
            } else {
                field.set(data, paramIn.get(name));
            }
        } catch (IllegalAccessException e) {
        }
    }

    /**
     * 对象A转为对象B (类)
     */
    public static <T, U> U copyBean(T from, U to) {
        if (null == from) {
            return null;
        }
        BeanUtils.copyProperties(from, to);
        return to;
    }

    /**
     * 对象A集合转为对象B集合
     */
    public static <T, U> List<U> copyList(List<T> fromList, Class<U> toClz) {
        List<U> toList = new ArrayList<U>();
        if (fromList != null && !fromList.isEmpty()) {
            fromList.forEach(from -> {
                U to;
                try {
                    to = toClz.newInstance();
                } catch (Exception e) {
                    throw new BusinessException(e);
                }
                BeanUtils.copyProperties(from, to);
                toList.add(to);
            });
        }
        return toList;
    }

//    public static void main(String[] args) {
//        JSONObject paramIn = new JSONObject();
//        paramIn.put("communityId", "123456");
//        paramIn.put("priceType", "1");
//        paramIn.put("priceStatic", "1.2345");
//        paramIn.put("penaltyDays", 100);
//        BillConfigDto dto = new BillConfigDto();
//        SpringBeanSupport.fillDtoFromJson(paramIn, dto);
//        System.out.println(dto);
//    }

}
